/**
 *
 * User: rendong
 * Date: 12-9-6
 * Time: 下午10:21
 * $Id: GImageSprite.js 11 2012-09-06 15:55:53Z rendong237@gmail.com $
 */


function GImageSprite(width, height){
    this.width = width;
    this.height = height;
}

GImageSprite.extend(new GDisplayObject());


GImageSprite.prototype.image = null;
GImageSprite.prototype.currentFrame = 0;
GImageSprite.prototype.playing = true;

/**
 * 存放帧序列
 * @type {Array}
 * @private
 */
GImageSprite.prototype._images = [];

/**
 * 添加地址
 * @param url
 */
GImageSprite.prototype.addUrl = function(url)
{
    var image = new GImage();
    image.loadImage(url);

    this._images.push(image);
}

/**
 * 添加image对象
 * @param image
 */
GImageSprite.prototype.addImage = function(image)
{
    this._images.push(image);
}

/**
 * 渲染图片
 * @param dirtyRect
 */
GImageSprite.prototype.render = function(dirtyRect)
{
    if (this._images.length == 0 || this.playing == false)
        return;

    if (this.image != null && this.currentFrame == 0){
        // 派发事件，播放完毕
        this.dispatchEventByType(GEvent.FINISH_FRAME);
    }

    this.image = this._images[this.currentFrame];

    if (this.image!= null && this.image.finish == true && this.visible && this.alpha > 0){
        this.stage.context.drawImage(this.image.image,this.x, this.y, this.width, this.height);

        this.currentFrame++;

        if (this.currentFrame == this._images.length){
            this.currentFrame = 0;
        }
    }
}

/**
 * 设置是否播放
 */
GImageSprite.prototype.setRun = function(play)
{
    this.playing = !!play;
}